package jsu.yym.tiktok.controller;

import jsu.yym.tiktok.pojo.Like;
import jsu.yym.tiktok.pojo.ResponseResult;
import jsu.yym.tiktok.pojo.Video;
import jsu.yym.tiktok.mapper.LikeMapper;
import jsu.yym.tiktok.mapper.VideoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

import static com.mysql.cj.conf.PropertyKey.logger;

@CrossOrigin
@RestController
public class LikeController {
    @Autowired
    private LikeMapper likeMapper;
    @Autowired
    private VideoMapper videoMapper;



    //用户点赞
    @PostMapping("/like/update/{videoId}")
    public ResponseResult addLike(@PathVariable int videoId)
    {
        String id = null;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null)
            id = authentication.getName(); // 获取当前用户的id
        Like like =new Like(id,videoId);
        System.out.println(like.getUsername()+"   "+like.getVideo_id());
        //判断是否重复添加
        Like like1 = likeMapper.getLikeByUsernameAndPkId(id,videoId);
        if (!Objects.isNull(like1)) {
            return new ResponseResult(409,"重复");
        }
        //保存到数据库中
       likeMapper.insertLike(like);
        //更新相关视频数据
        Video video = videoMapper.getVideoByPkId(like.getVideo_id());
        int count2 = videoMapper.updateLikesByPkId(video.getLikes()+1,video.getPkId());
        if(count2<=0)
            return new ResponseResult(403,"失败");
        return new ResponseResult(200,"成功");
    }

    //取消点赞
    @PostMapping("/like/delete/{videoId}")
    public ResponseResult deleteLike(@PathVariable int videoId)
    {
        String id = null;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null)
            id = authentication.getName(); // 获取当前用户的id
        Like like =new Like(id,videoId);
        System.out.println(like.getUsername()+"   "+like.getVideo_id());
        //删除记录
         likeMapper.deleteLike(like.getUsername(),like.getVideo_id());
        //更新相关视频数据
        Video video = videoMapper.getVideoByPkId(like.getVideo_id());
        int count2 = videoMapper.updateLikesByPkId(video.getLikes()-1,video.getPkId());
        if(count2<=0)
            return new ResponseResult(403,"失败");
        return new ResponseResult(200,"成功");
    }

    //获取用户点赞过的作品
    @GetMapping("/like/get")
    public ResponseResult getLikeVideo()
    {
        String id = null;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null)
            id = authentication.getName(); // 获取当前用户的id
        System.out.println(id);
        //获取用户点赞或藏的所有作品
        List<Integer> list = likeMapper.getVideoIdsByUsernameAndState(id);
        //获取对应视频的list集合
        List<Video> videos = videoMapper.getVideosByPkIds(list);
        //判断对象是否为null
        if (Objects.isNull(videos)) {
            return new ResponseResult(409,"该用户还没有点赞的视频");
        }
        return new ResponseResult(200,"获取成功",videos);

    }



}
